package com.elong.businesstravel.modules.home.dialog;

import java.util.Arrays;

import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.modules.home.adapter.SimpleListAdapter;
import com.elong.businesstravel.modules.home.adapter.SimpleListDialogAdapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.CacheManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 酒店排序
 * 
 * @author lenovo
 * 
 */
public class HotelSortsDialog extends Dialog {

	private Context mContext;
//	private ELongBusinessTravelApplication mApplication;
	private ListView mListView;
	private SimpleListDialogAdapter mSimpleListAdapter;
	private OnItemClickListener mOnItemClickListener;

	/** 酒店排序方式，附近酒店和选择酒店的排序方式不一样 */
	private String[] mHotelSort;
	private int mSelectSort;

	public HotelSortsDialog(Context context) {
		super(context);
		init(context);
	}

	public HotelSortsDialog(Context context, int theme, String[] hotelSort, int selectSort) {
		super(context, theme);
		mHotelSort = hotelSort;
		mSelectSort = selectSort;
		init(context);
	}

	public HotelSortsDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	private void init(Context context) {
		Window window = getWindow();
		window.setGravity(Gravity.BOTTOM);

		mContext = context;
//		mApplication = (ELongBusinessTravelApplication) mContext.getApplicationContext();

		setContentView(R.layout.dialog_hotel_sort);

		mListView = (ListView) findViewById(R.id.listView);
		mSimpleListAdapter = new SimpleListDialogAdapter(
				mContext,
				Arrays.asList(mHotelSort),
				mHotelSort[-1 == mSelectSort ? 0 : mSelectSort]);
		mListView.setAdapter(mSimpleListAdapter);

		setTitleNavBar();

		setListener();
		
		setCanceledOnTouchOutside(true);
	}

	protected void setListener() {

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String sortsString = (String) mSimpleListAdapter
						.getItem(position);
				// mApplication.mHotelSearchCondition.sorts = position;
				if(null != mOnItemClickListener){
					mOnItemClickListener.onItemClick(parent, view, position, id);
				}
			}
		});
	}

	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("选择排序");
		titleNavBarView.setTitleBackground(R.color.bg_title_dialog);
		titleNavBarView.setLeftButtonImageString(0, "取消", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				cancel();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	public void setOnItemListener(OnItemClickListener onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}
}
